﻿using System;
using System.Configuration;
using System.Data.SqlClient;
using VipCo.Data;
using VipCo.Entities;

public partial class Chitiet_baiviet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.Params["Id"];
            if (id != null)
            {
                HienthiThongtinBaiviet(Convert.ToInt32(id));
            }

            if (Session["TP"] == null)
            {
                Session["TP"] = Common.TP_Danang;
            }
        }
    }

    protected void HienthiThongtinBaiviet(int idBaiviet)
    {
        int totalRows = 0;
        string whereClause = "Id = " + idBaiviet + " and TrangThai = 1";

        TList<TinTuc> entTintuc = DataRepository.TinTucProvider.GetPaged(whereClause, "Id DESC", 0, 1, out totalRows);

        if (entTintuc.Count > 0)
        {
            // Thiet lap du lieu hien thi
            int _view = 0;

            //
            HienthiBaivietlienquan(entTintuc[0].IdNhomTin, entTintuc[0].IdSubTin);

            lbTieude.Text = entTintuc[0].TieuDe;
            lbTieude1.Text = entTintuc[0].TieuDe;
            ltrTieude.Text = entTintuc[0].TieuDe;
            lbNgaycapnhat.Text = entTintuc[0].NgayCapNhat.ToLongDateString();
            imgBaiviet.ImageUrl = "~/Upload/TinTuc/" + (entTintuc[0].AnhDaiDien == "0" ? "no_image.jpg" : entTintuc[0].AnhDaiDien);
            lbLuotxem.Text = entTintuc[0].LuotXem.ToString();
            _view = entTintuc[0].LuotXem;
            lbNoidung.Text = Server.HtmlDecode(entTintuc[0].NoiDung);
            lbNguon.Text = entTintuc[0].TacGia;

            // Thuc hien cap nhat luot xem cua bai viet
            string query = string.Empty;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
            conn.Open();
            _view += 1;
            query = string.Format("UPDATE TinTuc SET LuotXem = '{0}' where Id = '{1}'", _view, idBaiviet);
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = query;
            sqlCommand.ExecuteNonQuery();
            conn.Close();

        }
    }
    public void HienthiBaivietlienquan(int idNhomtin, int idSubNhomtin)
    {
        int totalRows = 0;
        string whereClause = "IdNhomTin = " + idNhomtin + " and IdSubTin = " + idSubNhomtin + " and TrangThai = 1" ;

        TList<TinTuc> tlistTintuc = DataRepository.TinTucProvider.GetPaged(whereClause, "NgayCapNhat DESC", 0, 10, out totalRows);

        if (tlistTintuc.Count > 0)
        {
            dlSubtintuc.DataSource = tlistTintuc;
            dlSubtintuc.DataBind();
        }
    }
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }
}